package thread;

import java.sql.SQLOutput;
import java.util.Random;

public class Test1 {
    public static final int ARRAY_size=10000000;
    public static int[]array=new int[ARRAY_size];

    private static volatile long evenSum=0;
    private static volatile long oddSum=0;

    public static void main(String[] args) throws InterruptedException {
        Random random=new Random();
        for (int i=0;i<ARRAY_size;i++){
            array[i]=random.nextInt(100)+1;//1-100随机数
        }

        long starTime= System.currentTimeMillis();
        Thread evenThread=new Thread(()->{
            for (int i=0;i<ARRAY_size;i+=2){
                evenSum+=array[i];
            }
        });
        Thread oddThread=new Thread(()->{
            for (int i=1;i<ARRAY_size;i+=2){
                oddSum+=array[i];
            }
        });

        evenThread.start();
        oddThread.start();

        evenThread.join();
        oddThread.join();

        long totalSum=evenSum+oddSum;
        long endTime=System.currentTimeMillis();

        System.out.println("偶数下标和："+evenSum);
        System.out.println("奇数下标和："+oddSum);
        System.out.println("总和："+totalSum);
        System.out.println("执行时间："+(endTime-starTime)+"毫秒");

    }
}
